//
//  LoopBarButtonItem+Extension.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/9.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit

extension UIBarButtonItem{

    //给方法的参数设置默认值,调用方法时可以省略存在默认值的参数
    convenience init(title:String = "",imageName:String? = nil,normalColor:UIColor,highLightedColor:UIColor?,target:Any?,action:Selector?){
        self.init()
        let btn = UIButton()
        //设置文字
        btn.setTitle(title, for: .normal)
        //设置颜色
        btn.setTitleColor(normalColor, for: .normal)
        if let highColor = highLightedColor{
        
            btn.setTitleColor(highColor, for: .highlighted)
        }
        
        //判断图片是否为空
        //为按钮设置图片
        if let img = imageName{
        btn.setImage(UIImage(named:img), for: .normal)
            btn.setImage(UIImage(named:img + "_highlighted"), for: .highlighted)
        }
        
        //添加按钮点击事件
        if let ac = action {
        btn.addTarget(target, action: ac, for: .touchUpInside)
        }
        
        btn.sizeToFit()
        self.customView = btn
    }


}




